<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Amitav Roy
 * Date: 1/30/13
 * Time: 9:19 AM
 * To change this template use File | Settings | File Templates.
 */
class Blocks_model extends CI_Model {
  // calling the construct
  public function __construct() {
    parent::__construct();
  }

  // latest web articles block
  public function get_block($block_name, $count = 5) {
    return $this->give_block_result($block_name, $count);
  }

  // all block handler
  protected function give_block_result($block_name, $count) {
    switch ($block_name) {
      // latest on the web block
      case 'latest_web':
        return $this->latest_web_result($count);
        break;

      // latest trip logs block
      case 'latest_triplogs':
        return $this->latest_trip_log($count);
        break;

      // default
      default:
        return false;
    }
  }

  // building the logic for the latest web block
  protected function latest_web_result($count) {
    $vid = 2; // article section
    $name = 'Web'; // term name

    //$this->db->select();
    $this->db->select('n.title, n.nid');
    $this->db->from('node as n');

    $this->db->join('taxonomy_index as ti', 'ti.nid=n.nid');
    $this->db->join('taxonomy_term_data as td', 'td.tid=ti.tid');

    $this->db->where('n.status', 1);
    $this->db->where('td.name',$name);
    $this->db->where('td.vid', $vid);

    $this->db->order_by('n.created', 'desc');
    $this->db->limit($count);

    $query = $this->db->get();
    $result = $query->result_array();
    if ($result)
      return $result;
    else
      return false;
  }

  // building the logic for the latest trip log block
  protected function latest_trip_log($count) {
    $vid = 2; // article section
    $name = 'Travel'; // term name

    //$this->db->select();
    $this->db->select('n.title, n.nid');
    $this->db->from('node as n');

    $this->db->join('taxonomy_index as ti', 'ti.nid=n.nid');
    $this->db->join('taxonomy_term_data as td', 'td.tid=ti.tid');

    $this->db->where('n.status', 1);
    $this->db->where('td.name',$name);
    $this->db->where('td.vid', $vid);

    $this->db->order_by('n.created', 'desc');
    $this->db->limit($count);

    $query = $this->db->get();
    $result = $query->result_array();

    if ($result)
      return $result;
    else
      return false;
  }


}
